package link_list

import (
	"strconv"
	"strings"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

func NewNode(val int, next *ListNode) *ListNode {
	return &ListNode{
		Val:  val,
		Next: next,
	}
}

func (l *ListNode) String() string {
	sb := strings.Builder{}
	node := l
	for node != nil {
		sb.WriteString(strconv.Itoa(node.Val))
		if node.Next != nil {
			sb.WriteString(",")
		}
		node = node.Next
	}
	return sb.String()
}

func MakeLinkList(val []int) *ListNode {
	var head *ListNode
	var r *ListNode
	for _, v := range val {
		node := NewNode(v, nil)
		if head == nil {
			head = node
			r = head
		} else {
			head.Next = node
			head = head.Next
		}
	}
	return r
}

type Node struct {
	Val    int
	Next   *Node
	Random *Node
}
